const mongoose = require('mongoose');

const subCategorySchema = new mongoose.Schema({
  name: {
    type: String,
    required: true,
    trim: true
  },
  // 关联的前端
  parent: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'TopCategory',
    required: true
  },
  image: {
    type: String,
    required: true,
    default: ''
  }
}, {
 timestamps: true,
 toJSON: { virtuals: true },
 toObject: { virtuals: true }
});

// 虚拟字段：关联的大分类（父分类）
subCategorySchema.virtual('parentInfo', {
 ref: 'TopCategory',
 localField: 'parent',
 foreignField: '_id',
 justOne: true
});

const SubCategory = mongoose.model('SubCategory', subCategorySchema);

module.exports = SubCategory;